package game.view.player
{
	import asset.testPlayer1Asset;
	
	import bitmap.BitmapAnimation;
	import bitmap.BitmapClipList;
	import bitmap.BitmapFrameObjectType;
	import bitmap.BitmapLayer;
	import bitmap.BitmapMovieClip;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import game.events.GamePlayerEvent;
	import game.vo.GamePlayerVO;
	
	import iso.IsoPoint;
	import iso.IsoSprite;
	import iso.IsoUtils;
	
	import road.AStar;
	
	public class GamePlayerBase extends IsoSprite
	{
		private var _gamePlayerVO:GamePlayerVO;//玩家实体数据
		private var _mapAStar:AStar;//A星寻路类
		private var _roadArray:Array;//地图路点
		private var _targetPoint:IsoPoint;//行动目的点
		private var _mapCellSize:Number;//地图单元格大小
		private var _mapPathArray:Array;//行动路径
		private var _actionTimer:Timer;//用与寻路行动
		private var _mapMoveNode:Array;//当前移动路径节点
		private var _playerLayer:BitmapLayer;
		private var _playerBox:Sprite;
		
		/**
		 * 玩家基类 
		 * @param playerVO 玩家实体数据
		 * @param roadArray 地图路点
		 * @param mapCellSize 地图单元格大小
		 */
		public function GamePlayerBase(gamePlayerVO:GamePlayerVO, roadArray:Array, mapCellSize:Number)
		{
			_gamePlayerVO=gamePlayerVO;
			_roadArray=roadArray;
			_mapCellSize=mapCellSize;
			
			setView();
			setEvent();
		}
		
		private function setView():void
		{
			_mapAStar=new AStar(_roadArray);
			_actionTimer=new Timer(1000/25);
			
			_playerBox=new Sprite();
			addChild(_playerBox);
			_playerLayer=new BitmapLayer(1000, 600);
			var bitmapClipList:BitmapClipList=new BitmapClipList();
			var bitmapAnimation:BitmapAnimation = new BitmapAnimation(new testPlayer1Asset(), BitmapFrameObjectType.MOVIE_CLIP);
			var playerAsset:BitmapMovieClip = new BitmapMovieClip(bitmapAnimation);
			playerAsset.play();
			bitmapClipList.append(playerAsset);
			_playerLayer.append(bitmapClipList);
			_playerBox.addChild(_playerLayer);
			_playerBox.x=-playerAsset.currentFrame.width/2;
			_playerBox.y=-playerAsset.currentFrame.height;
		}
		
		private function setEvent():void
		{
			//帧听玩家行动目标点是否改变
			_gamePlayerVO.addEventListener(GamePlayerEvent.PLAYER_TARGET_POINT_CHANGE, __onPlayerTargetPointChange);
			_actionTimer.addEventListener(TimerEvent.TIMER, __onPlayerMove);
		}
		
		private function removeEvent():void
		{
			_gamePlayerVO.removeEventListener(GamePlayerEvent.PLAYER_TARGET_POINT_CHANGE, __onPlayerTargetPointChange);
			_actionTimer.removeEventListener(TimerEvent.TIMER, __onPlayerMove);
		}
		
		public function update():void
		{
			_playerLayer.render();
		}
		
		/**
		 * 当玩家行动目标点改变
		 */		
		private function __onPlayerTargetPointChange(evt:GamePlayerEvent):void
		{
			_targetPoint=IsoUtils.screenToSpace(_gamePlayerVO.targetPoint);//转换行动目标点为空间坐标
			_targetPoint.x=Math.floor(_targetPoint.x/_mapCellSize)*_mapCellSize;
			_targetPoint.y=Math.floor(_targetPoint.y/_mapCellSize)*_mapCellSize;
			_targetPoint.z=Math.floor(_targetPoint.z/_mapCellSize)*_mapCellSize;
			
			var targetGridPoint:Point=IsoUtils.spaceToScreen(_targetPoint);
			targetGridPoint.x=Math.floor(targetGridPoint.x/_mapCellSize);
			targetGridPoint.y=Math.floor(targetGridPoint.y/_mapCellSize);
			
			var boxGridPoint:Point=IsoUtils.spaceToScreen(position);
			boxGridPoint.x=Math.floor(boxGridPoint.x/_mapCellSize);
			boxGridPoint.y=Math.floor(boxGridPoint.y/_mapCellSize);
			
			//开始寻路
			_mapPathArray = _mapAStar.find(boxGridPoint.x, boxGridPoint.y, targetGridPoint.x, targetGridPoint.y);
			if (_mapPathArray != null && _mapPathArray.length > 0)
			{
				_mapPathArray.shift();
				_mapMoveNode=_mapPathArray[0] as Array;
				_actionTimer.start();
			}
		}
		
		/**
		 *玩家行动 
		 */		
		private function __onPlayerMove(evt:TimerEvent):void
		{
			if (_mapMoveNode && _mapPathArray)
			{
				position=IsoUtils.screenToSpace(new Point(_mapMoveNode[0]*_mapCellSize, _mapMoveNode[1]*_mapCellSize));
				_mapMoveNode = _mapPathArray.shift() as Array;
				dispatchEvent(new GamePlayerEvent(GamePlayerEvent.PLAYER_POINT_CHANGE));
			}
			else
			{
				_actionTimer.stop();
				dispatchEvent(new GamePlayerEvent(GamePlayerEvent.PLAYER_GOTO_TARGET_POINT));
			}
			
			update();
		}
		
		public function get gamePlayerVO():GamePlayerVO
		{
			return _gamePlayerVO;
		}
		
		public function set gamePlayerVO(value:GamePlayerVO):void
		{
			_gamePlayerVO = value;
		}
		
		override public function dispose():void
		{
			super.dispose();
			removeEvent();
			_gamePlayerVO=null;
			_mapAStar=null;
			_roadArray=null;
			_targetPoint=null;
			_mapPathArray=null;
			_actionTimer.stop();
			_actionTimer=null;
			_mapMoveNode=null;
			
			if(parent) parent.removeChild(this);
		}
	}
}